//
//  ChooseHotCarCell.m
//  CarUser
//
//  Created by 王森林 on 2018/12/7.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "ChooseHotCarCell.h"
#import "ChooseHotBrandCell.h"
#import "ChooseHotCarCollectionCell.h"
#import "ChooseResultViewController.h"
@interface ChooseHotCarCell()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) CommonTitleView *titleView;
@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) NSMutableArray *dataAry;
@end

@implementation ChooseHotCarCell


//- (void)prepareForReuse
//{
//    [super prepareForReuse];
//    while ([self.contentView.subviews lastObject] != nil)
//    {
//        [(UIView*)[self.contentView.subviews lastObject] removeFromSuperview];
//    }
//    
//}

- (void)setData:(id)data
{
    if (data) {
        [self.dataAry addObjectsFromArray:data];
        [self createUIWithData];
    }
    
}

-(void)createUIWithData
{
    
    [self.contentView removeAllSubviews];
    self.backgroundColor = [UIColor  clearColor];
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.titleView];
    self.titleView.titleName = @"热门车系";
    self.titleView.moreBtn.hidden = YES;
    [self createConstraint];
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *goodListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 52, BOUNDWIDTH, 82) collectionViewLayout:layout];
    goodListView.dataSource = self;
    goodListView.delegate = self;
    goodListView.backgroundColor = [UIColor whiteColor];
//    [goodListView registerClass:NSClassFromString(CATEGORY_GOODS_CELL_IDENTIFIER) forCellWithReuseIdentifier:CATEGORY_GOODS_CELL_IDENTIFIER];
    [goodListView registerClass:NSClassFromString(@"ChooseHotCarCollectionCell") forCellWithReuseIdentifier:@"ChooseHotCarCollectionCell"];
    goodListView.showsHorizontalScrollIndicator = NO;
    //    goodListView.clipsToBounds = NO;
//    self.goodListView = goodListView;
    [self.contentView addSubview:goodListView];
    
    
//    __block float x = 15;
//    __block float y = 0;
//    __block float btnWidth  = 100;
//    __block float spaceX = 10;
//    __block float btnHeight = 82;
//    [self.dataAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//
//        NSDictionary *dict = obj;
//        NSString *title = [dict objectForKey:@"title"];
//        NSString *imageName = [dict objectForKey:@"imageName"];
//
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.frame =  CGRectMake(x+idx*(btnWidth+spaceX), y, btnWidth, btnHeight);
//
//        UIImageView *logoImageView = [UIImageView new];
//        logoImageView.image = IMG(imageName);
//        [btn addSubview:logoImageView];
//        logoImageView.frame = CGRectMake(0, 0, btnWidth, 60);
//
//        UILabel *titleLabel = [UILabel new];
//        titleLabel.textColor = RGB(40, 40, 40);
//        titleLabel.font = FONT_SIZE(12);
//        titleLabel.text = title;
//        titleLabel.textAlignment = NSTextAlignmentCenter;
//        [btn addSubview:titleLabel];
//        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(logoImageView.mas_bottom).offset(10);
//            make.left.equalTo(btn.mas_left);
//            make.right.equalTo(btn.mas_right);
//            make.height.mas_equalTo(12);
//        }];
//
//        [self.scrollView addSubview:btn];
//
//    }];
//
}

-(void)createConstraint
{
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top);
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.height.mas_equalTo(42);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_top).offset(42);
        make.left.equalTo(self.bgView.mas_left);
        make.right.equalTo(self.bgView.mas_right);
        make.height.mas_equalTo(82);
    }];
    
    self.scrollView.contentSize = CGSizeMake(BOUNDWIDTH, 82);
    
}


//#pragma mark - BSNLCycleBannerViewDataSource, BSNLCycleBannerViewDelegate
#pragma mark - 代理方法 Delegate Methods
// 设置分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return   self.dataAry.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
 
        ChooseHotCarCollectionCell *cell = (ChooseHotCarCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ChooseHotCarCollectionCell" forIndexPath:indexPath];
       NSDictionary *dict  = self.dataAry[indexPath.row];
        [cell setImageWithName:[dict objectForKey:@"ZST000"] withTitle:[dict objectForKey:@"NAME00"]];
        return cell;
    
    
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = 100;
    CGFloat height = 82;

    return CGSizeMake(width, height);
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsZero;
}


// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

     NSDictionary *dict  = self.dataAry[indexPath.row];
    ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
    controller.type = @"2";
      controller.pushType = 1;
    controller.SERIES_ID = [dict objectForKey:@"SERIES_ID"];
    [[self  viewController].navigationController pushViewController:controller animated:YES];
}




- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor  whiteColor];
    }
    return _bgView;
    
}

- (CommonTitleView *)titleView
{
    if (!_titleView) {
        _titleView = [CommonTitleView new];
        
    }
    return _titleView;
    
}

//- (UIScrollView *)scrollView
//{
//    if (!_scrollView) {
//        _scrollView = [UIScrollView new];
//        _scrollView.scrollEnabled = YES;
//        _scrollView.backgroundColor = [UIColor redColor];
////        _scrollView.delegate = self;
//
//    }
//    return _scrollView;
//
//}

- (NSArray *)dataAry
{
    if (!_dataAry) {
        _dataAry = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataAry;
}


@end
